# coding: utf-8
from typing import Union

from PySide6.QtCore import Qt, QUrl, QSize
from PySide6.QtGui import QIcon, QFont, QColor
from PySide6.QtWidgets import QHBoxLayout, QVBoxLayout, QWidget, QFileDialog
from qfluentwidgets import IconWidget, FluentIconBase, TitleLabel, SimpleCardWidget, HyperlinkLabel, BodyLabel, \
    PrimaryPushButton, CaptionLabel, setFont, VerticalSeparator, TransparentToolButton, FluentIcon

from common import SingleIcon, runProcess


class StatisticsWidget(QWidget):
    """ Statistics widget """

    def __init__(self, title: str, value: str, parent=None):
        super().__init__(parent=parent)
        self.titleLabel = CaptionLabel(title, self)
        self.valueLabel = BodyLabel(value, self)
        self.vBoxLayout = QVBoxLayout(self)

        self.vBoxLayout.setContentsMargins(0, 0, 0, 0)
        self.vBoxLayout.addWidget(self.valueLabel, 0, Qt.AlignTop)
        self.vBoxLayout.addWidget(self.titleLabel, 0, Qt.AlignBottom)

        setFont(self.valueLabel, 18, QFont.DemiBold)
        self.titleLabel.setTextColor(QColor(96, 96, 96), QColor(206, 206, 206))

    def setTitle(self, title: str):
        self.titleLabel.setText(title)

    def setValue(self, value: str):
        self.valueLabel.setText(value)

    def value(self) -> str:
        return self.valueLabel.text()

    def titile(self) -> str:
        return self.titleLabel.text()


class AppInfoCard(SimpleCardWidget):
    """ App information card """

    def __init__(
            self,
            icon: Union[str, QIcon, FluentIconBase],
            title: str,
            content: str,
            home_url: str,
            home_text: str,
            parent=None
    ):
        super().__init__(parent)
        self.iconLabel = IconWidget(icon, self)
        self.iconLabel.setFixedSize(120, 120)
        self.nameLabel = TitleLabel(title, self)
        self.installButton = PrimaryPushButton(self.tr('选择Python解释器'), self)
        self.companyLabel = HyperlinkLabel(QUrl(home_url), home_text, self)
        self.installButton.setFixedWidth(160)

        self.scoreWidget = StatisticsWidget('解释器版本', '未知', self)
        self.separator = VerticalSeparator(self)
        self.commentWidget = StatisticsWidget('PyInstaller版本', '未知', self)

        self.descriptionLabel = BodyLabel(content, self)
        self.descriptionLabel.setWordWrap(True)

        self.sourceLabel = CaptionLabel(self.tr('请选择Python解释器'), self)
        self.sourceLabel.setTextColor(QColor(96, 96, 96), QColor(206, 206, 206))

        self.shareButton = TransparentToolButton(FluentIcon.SHARE, self)
        self.shareButton.setFixedSize(32, 32)
        self.shareButton.setIconSize(QSize(14, 14))

        self.hBoxLayout = QHBoxLayout(self)
        self.vBoxLayout = QVBoxLayout()
        self.topLayout = QHBoxLayout()
        self.statisticsLayout = QHBoxLayout()
        self.buttonLayout = QHBoxLayout()

        self.initLayout()
        self.setBorderRadius(8)
        # 信号连接
        self.installButton.clicked.connect(self.__select_interpreter)

    def initLayout(self):
        self.hBoxLayout.setSpacing(30)
        self.hBoxLayout.setContentsMargins(34, 24, 24, 24)
        self.hBoxLayout.addWidget(self.iconLabel)
        self.hBoxLayout.addLayout(self.vBoxLayout)

        self.vBoxLayout.setContentsMargins(0, 0, 0, 0)
        self.vBoxLayout.setSpacing(0)

        # name label and install button
        self.vBoxLayout.addLayout(self.topLayout)
        self.topLayout.setContentsMargins(0, 0, 0, 0)
        self.topLayout.addWidget(self.nameLabel)
        self.topLayout.addWidget(self.installButton, 0, Qt.AlignRight)

        # company label
        self.vBoxLayout.addSpacing(3)
        self.vBoxLayout.addWidget(self.companyLabel)

        # statistics widgets
        self.vBoxLayout.addSpacing(20)
        self.vBoxLayout.addLayout(self.statisticsLayout)
        self.statisticsLayout.setContentsMargins(0, 0, 0, 0)
        self.statisticsLayout.setSpacing(10)
        self.statisticsLayout.addWidget(self.scoreWidget)
        self.statisticsLayout.addWidget(self.separator)
        self.statisticsLayout.addWidget(self.commentWidget)
        self.statisticsLayout.setAlignment(Qt.AlignLeft)

        # description label
        self.vBoxLayout.addSpacing(20)
        self.vBoxLayout.addWidget(self.descriptionLabel)

        # button
        self.vBoxLayout.addSpacing(12)
        self.buttonLayout.setContentsMargins(0, 0, 0, 0)
        self.vBoxLayout.addLayout(self.buttonLayout)
        self.buttonLayout.addWidget(self.sourceLabel, 0, Qt.AlignLeft)
        self.buttonLayout.addWidget(self.shareButton, 0, Qt.AlignRight)

    def __select_interpreter(self):
        filename, _ = QFileDialog.getOpenFileName(self, self.tr('选择Python解释器'), '', 'Python Interpreter (*.exe)')
        if not filename:
            return
        self.sourceLabel.setText(filename)
        # 获取python版本
        python_version = runProcess(filename, ['-c', 'import platform; print(platform.python_version())'])
        self.scoreWidget.setValue(python_version)
        # 获取编译器版本
        name = self.nameLabel.text()
        if name == "PyInstaller":
            compiler_cmd = ['-m', 'PyInstaller', '--version']
        else:
            compiler_cmd = ['-m', 'nuitka', '--version']

        result = runProcess(filename, compiler_cmd)
        if result:
            self.commentWidget.setValue(result)
            return




class PyInstallerInstallCard(AppInfoCard):
    def __init__(self, parent=None):
        super().__init__(
            SingleIcon.Pyinstaller,
            "PyInstaller",
            "PyInstaller是一个开源的Python编译器，它可以将Python代码编译成可执行文件，并将其打包成可移植的格式，以便于分发和部署。",
            "https://www.pyinstaller.org/",
            self.tr("官方网站"),
            parent
        )


class NuitkaInstallCard(AppInfoCard):
    def __init__(self, parent=None):
        super().__init__(
            SingleIcon.Nuitka,
            "Nuitka",
            "Nuitka是一个开源的Python编译器，它可以将Python代码编译成可执行文件，并将其打包成可移植的格式，以便于分发和部署。",
            "https://nuitka.net/",
            self.tr("官方网站"),
            parent
        )
        self.commentWidget.setTitle(self.tr('Nuitka版本'))
